﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SalesCommand;


namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 更改退货通知单的审批状态处理状态
    /// </summary>
    public class UdpreturnShenPiStatusCommandHandler : IRequestHandler<UdpreturnShenPiStatusCommand, APIResult<int>>
    {

        private readonly IBaseRepository<return_notice_main> rRepo;

        public UdpreturnShenPiStatusCommandHandler(IBaseRepository<return_notice_main> rRepo)
        {
            this.rRepo = rRepo;
        }

        public Task<APIResult<int>> Handle(UdpreturnShenPiStatusCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = rRepo.GetAll().Result.Where(x => x.notice_id == request.notice_id).FirstOrDefault();
            if (res.ShenPiStatus == "待审核")
            {
                res.ShenPiStatus = "已审核";
                var r = rRepo.Update(res).Result;
                if (r > 0)
                {
                    result.data = r;
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    return Task.FromResult(result);
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = r;
                }

            }
            else if (res.ShenPiStatus == "已审核")
            {
                res.ShenPiStatus = "已完成--通过";
                var r = rRepo.Update(res).Result;
                if (r > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = r;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = r;
                }
            }

            return Task.FromResult(result);




        }
    }
}
